<?php

namespace app\api\controller;

use app\common\lib\Arr;
use app\common\lib\Show;
use think\response\Json;

class Category extends ApiBase
{

    public function getCategory(){
        $name = $this->request->param("name", "", "trim");
        $is_main = input('is_main',0,'intval');

        $flag = $is_main == 1;

        try {
            $data = (new \app\common\business\Category())->getCategoryByName($name,$flag);
        } catch (\Exception $e) {
            return Show::error([], $e->getMessage(), $e->getCode());
        }
        return Show::success($data);
    }

    public function getCategoryMultiple()
    {
        if (!$this->request->isPost()) {
            return Show::error([],"非法请求");
        }

        $name=$this->request->post('name');
        try {
            $data=(new \app\common\business\Category())->getCategoryMultipleByName($name);
        }catch (\Exception $e){
            return Show::error([], $e->getMessage(), $e->getCode());
        }
        return Show::success($data);
    }

    /**
     * @return Json
     */
    public function getCategoryByPid(): Json
    {
        $id=$this->request->get('id');
        try {
            $data=(new \app\common\business\Category())->getCategoryByPid($id);
        }catch (\Exception $e){
            return Show::error([],$e->getMessage(),$e->getCode());
        }
        return Show::success($data);
    }
}
